<template>
  <div class="home-container">
    <header class="header">
      <h1>消息提醒中心</h1>
      <div class="connection-status" :class="currentConnectionStatus">
        <span class="status-indicator"></span>
        <span>
          {{ currentConnectionStatus === 'connected' ? '已连接' :
            currentConnectionStatus === 'connecting' ? '连接中' : '已断开' }}
        </span>
      </div>
      <button class="new-alert-btn" @click="$router.push('/new-alert')">
        <i class="fas fa-plus"></i> 新增提醒
      </button>
      <button class="settings-btn" @click="$router.push('/settings')">
        <i class="fas fa-cog"></i>
      </button>
    </header>

    <main class="main-content">
      <div class="alerts-list">
        <h2>当前提醒</h2>
        <div v-if="activeAlerts.length === 0" class="empty-state">
          暂无活跃提醒
        </div>
        <div v-else class="alert-cards">
          <div class="alert-card" v-for="alert in activeAlerts" :key="alert.id">
            <div class="alert-header">
              <h3>{{ alert.title }}</h3>
              <button class="close-btn" @click="closeAlert(alert.id)">
                <i class="fas fa-times"></i>
              </button>
            </div>
            <div class="alert-content">
              {{ alert.content }}
            </div>
            <div class="alert-time">
              创建时间: {{ new Date(alert.createdAt).toLocaleString() }}
            </div>
          </div>
        </div>

        <h2 class="history-title">历史提醒</h2>
        <div v-if="historyAlerts.length === 0" class="empty-state">
          暂无历史提醒
        </div>
        <div v-else class="history-alerts">
          <div class="history-alert" v-for="alert in historyAlerts" :key="alert.id">
            <div class="history-alert-title">{{ alert.title }}</div>
            <div class="history-alert-content">{{ alert.content }}</div>
            <div class="history-alert-time">
              {{ new Date(alert.createdAt).toLocaleString() }}
            </div>
          </div>
        </div>
      </div>
    </main>
  </div>
</template>

<script setup>
import { onMounted, computed, watch } from 'vue'
import { useAppStore } from '../store'
import { storeToRefs } from 'pinia'
// 导入generateClientId函数
import { generateClientId } from '../utils/platformDetector';

const store = useAppStore()

// 关键修复：使用计算属性强制追踪 connectionStatus
const currentConnectionStatus = computed(() => {
  return store.connectionStatus
})

// 使用 storeToRefs 解构响应式属性
const {
  activeAlerts
} = storeToRefs(store)

// 保持 closeAlert 直接从 store 获取（方法不需要响应式）
const { closeAlert } = store

// 计算属性 - 历史提醒（非活跃状态的）
const historyAlerts = computed(() => {
  const activeIds = store.activeAlerts.map(a => a.id)
  return store.alerts.filter(alert => !activeIds.includes(alert.id))
    .sort((a, b) => new Date(b.createdAt) - new Date(a.createdAt))
})

watch(currentConnectionStatus, (newVal) => {
}, { immediate: true })

// 组件挂载时初始化
onMounted(() => {
  // 确保clientId已生成
  if (!store.settings.clientId) {
    store.settings.clientId = generateClientId();
    store.saveSettings(store.settings);
  }

  store.init();
})


</script>

<style scoped>
.home-container {
  display: flex;
  flex-direction: column;
  height: 100vh;
  background-color: #f5f5f5;
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 1rem 2rem;
  background-color: #2c3e50;
  color: white;
  box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}

.connection-status {
  display: flex;
  align-items: center;
  padding: 0.5rem 1rem;
  border-radius: 4px;
  font-weight: 500;
}

.status-indicator {
  display: inline-block;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  margin-right: 8px;
}

.connection-status.connected {
  background-color: rgba(46, 204, 113, 0.2);
  color: #2ecc71;
}

.connection-status.connected .status-indicator {
  background-color: #2ecc71;
}

.connection-status.connecting {
  background-color: rgba(241, 196, 15, 0.2);
  color: #f1c40f;
}

.connection-status.connecting .status-indicator {
  background-color: #f1c40f;
}

.connection-status.disconnected {
  background-color: rgba(231, 76, 60, 0.2);
  color: #e74c3c;
}

.connection-status.disconnected .status-indicator {
  background-color: #e74c3c;
}

.new-alert-btn, .settings-btn {
  background-color: #3498db;
  color: white;
  border: none;
  padding: 0.5rem 1rem;
  border-radius: 4px;
  cursor: pointer;
  font-size: 1rem;
  display: flex;
  align-items: center;
  gap: 8px;
}

.new-alert-btn:hover, .settings-btn:hover {
  background-color: #2980b9;
}

.settings-btn {
  margin-left: 1rem;
  padding: 0.5rem;
}

.main-content {
  flex: 1;
  padding: 2rem;
  overflow-y: auto;
}

.alerts-list {
  max-width: 1200px;
  margin: 0 auto;
}

.alert-cards {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 1rem;
  margin-bottom: 2rem;
}

.alert-card {
  background-color: white;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.1);
  padding: 1rem;
  transition: transform 0.2s;
}

.alert-card:hover {
  transform: translateY(-2px);
}

.alert-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 0.5rem;
}

.alert-header h3 {
  margin: 0;
  color: #2c3e50;
}

.close-btn {
  background: none;
  border: none;
  color: #e74c3c;
  cursor: pointer;
  font-size: 1.2rem;
}

.alert-content {
  margin-bottom: 1rem;
  color: #34495e;
  line-height: 1.5;
}

.alert-time {
  font-size: 0.8rem;
  color: #7f8c8d;
  text-align: right;
}

.history-title {
  margin-top: 2rem;
  padding-top: 1rem;
  border-top: 1px solid #ddd;
}

.history-alerts {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.history-alert {
  background-color: white;
  border-radius: 4px;
  padding: 0.8rem;
  border-left: 3px solid #95a5a6;
}

.history-alert-title {
  font-weight: 600;
  color: #2c3e50;
  margin-bottom: 0.3rem;
}

.history-alert-content {
  color: #34495e;
  font-size: 0.9rem;
  margin-bottom: 0.3rem;
}

.history-alert-time {
  font-size: 0.8rem;
  color: #7f8c8d;
}

.empty-state {
  text-align: center;
  padding: 2rem;
  color: #7f8c8d;
  background-color: white;
  border-radius: 8px;
  margin-bottom: 1rem;
}
</style>
